<?php

namespace app\enterprise\controller;

use think\Controller;
use think\Request;
use think\facade\Http;
use app\model\staff\Staff;
use GuzzleHttp\Client;

class PushService extends Enterprise
{
    public function batchPush(array $cids, string $title, string $content, $data = [])
    {
        $url = "https://fc-mp-8ee293ce-a384-4c91-a04c-02ff0c671069.next.bspapp.com/api"; // 云函数URL化地址
        $data = [
            "push_clientid" => $cids, // cid数组
            "force_notification" => true, //填写true，客户端就会对在线消息自动创建“通知栏消息”。
            "title" => $title,
            "content" => $content,
            // "payload" => json_encode(["text" => "payload_text"]) // 自定义数据
            "payload" => $data
        ];

        $result = json_decode(file_get_contents($url, false, stream_context_create([
            'http' => [
                'method' => 'POST',
                'header' => 'Content-Type: application/json',
                'content' => json_encode($data)
            ]
        ]), true));

        return $result;
    }
    // 培训上线通知
    public function sendCultivateOnline($title, $content, $data = [])
    {
        // $this->adminId;
        // $this->adminInfo;
        // $this->auth;
        $adminInfo = $this->adminInfo;

        $userPushIds = Staff::where([
            ['company_id', '=', $adminInfo['company_id']],
            ['status', '=', 1],
            ['job_status', '=', 1],
            ['push_id', '<>', ''],
        ])->column('push_id');
        $msg_title = $title;
        $msg_content = $content;
        $this->batchPush($userPushIds, $msg_title, $msg_content, $data);
    }
}
